package com.dindang.maintenance.mvp.presenter;

import android.app.Application;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.ViewTreeObserver;

import com.dindang.maintenance.app.utils.AddPicUtils;
import com.dindang.maintenance.mvp.ui.adapter.custom.GridImageAdapter;
import com.dindang.maintenance.mvp.ui.widget.manager.FullyGridLayoutManager;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.dindang.maintenance.mvp.contract.CompleteContract;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;


@ActivityScope
public class CompletePresenter extends BasePresenter<CompleteContract.Model, CompleteContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private GridImageAdapter mGridImageAdapter;
    private AddPicUtils mAddPicUtils;
    private int imageWidth = 0;
    private int maxSelectNum = 6;// 图片最大可选择数

    @Inject
    public CompletePresenter(CompleteContract.Model model, CompleteContract.View rootView) {
        super(model, rootView);
    }

    public void initView() {
        initSelectImage();
    }

    private void initSelectImage() {
        mRootView.getTvNumberPictures().setText("可选，最多支持添加" + maxSelectNum + "张图片");
        setPicRemainNumTip(0);
        mAddPicUtils = new AddPicUtils(mRootView.getActivity());
        mAddPicUtils.setSelectMax(maxSelectNum);
        mGridImageAdapter = new GridImageAdapter(mRootView.getRvPictures().getContext(), mAddPicUtils.onAddPicClickListener);
        mGridImageAdapter.setList(mAddPicUtils.selectList);
        mGridImageAdapter.setSelectMax(maxSelectNum);
        mRootView.getRvPictures().setAdapter(mGridImageAdapter);
        FullyGridLayoutManager manager = new FullyGridLayoutManager(mRootView.getRvPictures().getContext(), 4, GridLayoutManager.VERTICAL, false);
        mRootView.getRvPictures().setLayoutManager(manager);

        ViewTreeObserver vto = mRootView.getRvPictures().getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if(imageWidth == 0){
                    imageWidth = mRootView.getRvPictures().getMeasuredWidth();
                    mGridImageAdapter.setImageWidth(imageWidth);
                }
                return true;
            }
        });

        mGridImageAdapter.setOnRemainNumListener(new GridImageAdapter.OnRemainNumListener() {
            @Override
            public void onItemClick(int remainNum) {
                setPicRemainNumTip(remainNum);
            }
        });
    }

    /**
     * activity回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != mRootView.getActivity().RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                mAddPicUtils.selectList = PictureSelector.obtainMultipleResult(data);
                mGridImageAdapter.setList(mAddPicUtils.selectList);
                mGridImageAdapter.notifyDataSetChanged();
                setPicRemainNumTip(mAddPicUtils.selectList.size());
                break;
        }
    }

    private void setPicRemainNumTip(int remain){
        mRootView.getTvPicturesRemainingAmount().setText(remain + "/" + maxSelectNum);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

        this.mGridImageAdapter = null;
        this.mAddPicUtils = null;
    }

}
